package com.example.demo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Created by IntelliJ IDEA.
 * User: zm
 * Date: 2023/5/29
 */
@Aspect //告诉框架我是一个切面类
@Component //随着框架启动而启动
public class UserAspect {
    /**
     * 这是一个切点
     */
    @Pointcut("execution(* com.example.demo.controller.UserController.*(..))")
    public void pointcut(){

    }

    /**
     * 定义通知 前置通知和后置通知
     */
    @Before("pointcut()")
    public void doBeforeAdvice(){
        System.out.println("执行了前置通知~");
    }

    @After("pointcut()")
    public void AfterAdvice(){
        System.out.println("执行了后置通知~");
    }

    /**
     * 环绕通知
     * @param joinPoint
     * @return
     */
    @Around("pointcut()")
    public Object afterAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("进入环绕通知了");
        Object obj = null;
        obj = joinPoint.proceed();
        System.out.println("推出环绕通知");
        return obj;
    }

}
